package com.account.book.controller;


import com.account.book.common.context.UserTokenContextHolder;
import com.account.book.common.response.BaseResult;
import com.account.book.common.response.ResultUtils;
import com.account.book.request.BookUserQueryReqVo;
import com.account.book.request.UserApplyBookReqVo;
import com.account.book.response.BookUserQueryVo;
import com.account.book.response.BookUserResVo;
import com.account.book.service.BookUserService;
import com.account.book.vo.BookUserEditVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang-hw
 * @className BookUserController
 * @description 账本用户接口
 * @date 2023/11/09 10:25:27
 */
@Api(tags = {"账本用户接口"})
@RestController
@RequestMapping("/api/book/user")
public class BookUserController {

    @Autowired
    private BookUserService bookUserService;

    /**
     * @param @param bookId
     * @return @return {@link Object }
     * @author liang-hw
     * @description 查询账本下的用户列表
     * @date 2023/11/09 10:25:46
     */
    @ApiOperation(value = "查询账本下的用户列表", notes = "查询账本下的用户列表")
    @GetMapping("/get")
    public BaseResult<BookUserResVo> getBookUser(@RequestParam("bookId") String bookId) {
        return ResultUtils.success(bookUserService.getBookUser(bookId));
    }

    /**
     * @param @param bookId
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 判断用户是否有该账本权限
     * @date 2024/01/09 14:16:20
     */
    @ApiOperation(value = "判断用户是否有该账本权限", notes = "判断用户是否有该账本权限")
    @GetMapping("/isBookAuthor")
    public BaseResult isBookAuthor(@RequestParam("bookId") String bookId) {
        // 获取用户id
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        BookUserQueryVo bookUserQueryVo = bookUserService.isBookAuthor(bookId, userId);
        return ResultUtils.success(bookUserQueryVo);
    }

    /**
     * @param @param bookId
     * @return @return {@link BaseResult }<{@link BookUserQueryVo }>
     * @author liang-hw
     * @description 查询账本下的用户信息
     * @date 2023/12/04 16:47:52
     */
    @ApiOperation(value = "查询账本下的用户信息", notes = "查询账本下的用户信息")
    @GetMapping("/get/bookUserInfo")
    public BaseResult<BookUserQueryVo> getBookUserInfo(@RequestParam("bookId") String bookId) {
        // 获取用户id
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        return ResultUtils.success(bookUserService.getBookUserInfo(bookId, userId));
    }

    /**
     * @param @param bookUserQueryReqVo
     * @return @return {@link BaseResult }<{@link BookUserQueryVo }>
     * @author liang-hw
     * @description 修改当前账本预算
     * @date 2024/01/10 16:44:07
     */
    @ApiOperation(value = "修改当前账本预算", notes = "修改当前账本预算")
    @PostMapping("/editBudget")
    public BaseResult editBudget(@RequestBody BookUserQueryReqVo bookUserQueryReqVo) {
        // 获取用户id
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.editBudget(bookUserQueryReqVo, userId);
        return ResultUtils.success();
    }

    /**
     * @param @param userApplyBookReqVo
     * @return @return {@link Object }
     * @author liang-hw
     * @description 用户从搜索中申请加入账本请求
     * @date 2023/11/09 10:26:15
     */
    @ApiOperation(value = "用户从搜索中申请加入账本请求", notes = "用户从搜索中申请加入账本请求")
    @PostMapping("/send/apply")
    public BaseResult<BookUserResVo> sendApply(@RequestBody UserApplyBookReqVo userApplyBookReqVo) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        BookUserResVo bookUserResVo = bookUserService.sendApply(userApplyBookReqVo, userId);
        return ResultUtils.success(bookUserResVo);
    }

    /**
     * @param @param userApplyBookReqVo
     * @return @return {@link Object }
     * @author liang-hw
     * @description 同意用户加入账本
     * @date 2023/11/09 10:26:37
     */
    @ApiOperation(value = "同意用户加入账本", notes = "同意用户加入账本")
    @PostMapping("/agree")
    public BaseResult agree(@RequestBody UserApplyBookReqVo userApplyBookReqVo) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.agree(userApplyBookReqVo, userId);
        return ResultUtils.success();
    }

    /**
     * @param @param userApplyBookReqVo
     * @return @return {@link Object }
     * @author liang-hw
     * @description 拒绝用户加入账本
     * @date 2023/11/09 10:27:30
     */
    @ApiOperation(value = "拒绝用户加入账本", notes = "拒绝用户加入账本")
    @PostMapping("/reject")
    public BaseResult reject(@RequestBody UserApplyBookReqVo userApplyBookReqVo) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.reject(userApplyBookReqVo, userId);
        return ResultUtils.success();
    }


    /**
     * @param
     * @return @return {@link Object }
     * @author liang-hw
     * @description 获取待审核的数量
     * @date 2023/11/09 10:27:46
     */
    @ApiOperation(value = "获取待审核的数量", notes = "获取待审核的数量")
    @GetMapping("/auditCount")
    public BaseResult getAuditCount() {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        return ResultUtils.success(bookUserService.getAuditCount(userId));
    }


    /**
     * @param
     * @return @return {@link Object }
     * @author liang-hw
     * @description 查询所有申请加入的用户
     * @date 2023/11/09 10:27:59
     */
    @ApiOperation(value = "查询所有申请加入的用户", notes = "查询所有申请加入的用户")
    @GetMapping("/auditAll")
    public BaseResult<List<BookUserQueryVo>> getAuditAll() {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        return ResultUtils.success(bookUserService.getAuditAll(userId));
    }

    /**
     * @param
     * @return @return {@link Object }
     * @author liang-hw
     * @description 查询我的申请
     * @date 2023/11/09 10:28:13
     */
    @ApiOperation(value = "查询我的申请", notes = "查询我的申请")
    @GetMapping("/my/apply")
    public BaseResult<List<BookUserQueryVo>> myApply() {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        return ResultUtils.success(bookUserService.myApply(userId));
    }

    /**
     * @param @param bookId
     * @return @return {@link Object }
     * @author liang-hw
     * @description 查询账本申请加入的用户
     * @date 2023/11/09 10:28:26
     */
    @ApiOperation(value = "查询账本申请加入的用户", notes = "查询账本申请加入的用户")
    @GetMapping("/audit")
    public BaseResult<BookUserResVo> bookAudit(@RequestParam("bookId") String bookId) {
        return ResultUtils.success(bookUserService.bookAudit(bookId));
    }


    /**
     * @param @param bookUserEditVo
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 编辑成员
     * @date 2023/11/09 10:28:43
     */
    @ApiOperation(value = "编辑成员", notes = "编辑成员")
    @GetMapping("/edit")
    public BaseResult editBookUser(@RequestBody BookUserEditVo bookUserEditVo) {
        bookUserService.editBookUser(bookUserEditVo);
        return ResultUtils.success();
    }


    /**
     * @param @param bookId
     * @param userId
     * @return @return {@link Object }
     * @author liang-hw
     * @description 删除成员
     * @date 2023/11/09 10:29:11
     */
    @ApiOperation(value = "删除成员", notes = "删除成员")
    @GetMapping("/remove")
    public BaseResult remove(@RequestParam("bookId") String bookId, @RequestParam("editUserId") String editUserId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.remove(bookId, editUserId, userId);
        return ResultUtils.success();
    }


    /**
     * @param @param bookId
     * @param userId
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 添加成员为管理员
     * @date 2023/11/09 10:29:56
     */
    @ApiOperation(value = "添加成员为管理员", notes = "添加成员为管理员")
    @Deprecated
    @GetMapping("/addAdmin")
    public BaseResult addAuth(
            @RequestParam("bookId") String bookId, @RequestParam("editUserId") String editUserId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.addAuth(bookId, editUserId, userId);
        return ResultUtils.success();
    }

    /**
     * @param @param bookId
     * @param userId
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 移除管理员
     * @date 2023/11/09 10:30:35
     */
    @ApiOperation(value = "移除管理员", notes = "移除管理员")
    @Deprecated
    @GetMapping("/removeAdmin")
    public BaseResult removeAuth(@RequestParam("bookId") String bookId, @RequestParam("editUserId") String editUserId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.removeAuth(bookId, editUserId,userId);
        return ResultUtils.success();
    }


    /**
     * @param @param bookUserEditVo
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 修改用户，目前只支持修改用户名称跟头像
     * @date 2023/11/09 10:30:54
     */
    @ApiOperation(value = "修改用户，目前只支持修改用户名称跟头像", notes = "修改用户，目前只支持修改用户名称跟头像")
    @PostMapping("/update")
    public BaseResult update(@RequestBody BookUserEditVo bookUserEditVo) {
        bookUserService.update(bookUserEditVo);
        return ResultUtils.success();
    }


    /**
     * @param @param bookId
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 用户自己退出账本
     * @date 2023/11/09 10:31:19
     */
    @ApiOperation(value = "用户自己退出账本", notes = "用户自己退出账本")
    @GetMapping("/signOutBook")
    public BaseResult signOutBook(@RequestParam("bookId") String bookId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookUserService.signOutBook(bookId, userId);
        return ResultUtils.success();
    }
}
